import numpy as np
import csv
from numba import njit
from numba.typed import List


@njit(target = 'cpu', parallel = True)
def combine_kernels(kernel_list, weight_v):
    p = np.shape(kernel_list[0])[0]
    q = np.shape(kernel_list[0])[1]
    K = np.zeros((p, q))
    n = len(weight_v)
    for i in range(n):
        K = K + kernel_list[i] * weight_v[i]
    return K


dataset_name = ['Antifp_Main', 'Antifp_DS1', 'Antifp_DS2']
mkl_name = ['HSIC', 'FKL', 'TKA', 'HKA', 'CKA']
for ds in range(3):
    name_ds = dataset_name[ds]
    print(name_ds)
    for mkl in range(1):
        name_mkl = mkl_name[mkl]
        print('MKL: ', name_mkl)
        #train kernel
        f1 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_train_gaussian/KM_gaussian_188-bit_train.csv', delimiter = ',')
        f2 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_train_gaussian/KM_gaussian_AAC_train.csv', delimiter = ',')
        f3 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_train_gaussian/KM_gaussian_ASDC_train.csv', delimiter = ',')
        f4 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_train_gaussian/KM_gaussian_CKSAAP_train.csv', delimiter = ',')
        f5 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_train_gaussian/KM_gaussian_DPC_train.csv', delimiter = ',')
        f6 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_train_tanimoto/KM_tanimoto_ASDC_train.csv', delimiter = ',')
        f7 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_train_tanimoto/KM_tanimoto_CKSAAP_train.csv', delimiter = ',')

        weight_v = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix_G5+T2/' + name_ds +'/KM_train/' + name_mkl + '_weight.txt', delimiter = ',', usecols = 0)
        print(weight_v)
        kernel_list = [f1, f2, f3, f4, f5, f6, f7]
        typed_kernel_list = List()
        [typed_kernel_list.append(x) for x in kernel_list]
        K = combine_kernels(typed_kernel_list, weight_v)
        print(K)

        with open('D:/Study/Bioinformatics/AFP/kernel_matrix_G5+T2/' + name_ds +'/KM_train/combine_' + name_mkl + '_train.csv', 'w', newline='') as csvfile:
            writer = csv.writer(csvfile)
            for row in K:
                writer.writerow(row)
            csvfile.close()
        #test kernel
        f1 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_test_gaussian/KM_gaussian_188-bit_test.csv', delimiter = ',')
        f2 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_test_gaussian/KM_gaussian_AAC_test.csv', delimiter = ',')
        f3 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_test_gaussian/KM_gaussian_ASDC_test.csv', delimiter = ',')
        f4 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_test_gaussian/KM_gaussian_CKSAAP_test.csv', delimiter = ',')
        f5 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_test_gaussian/KM_gaussian_DPC_test.csv', delimiter = ',')
        f6 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_test_tanimoto/KM_tanimoto_ASDC_test.csv', delimiter = ',')
        f7 = np.loadtxt('D:/Study/Bioinformatics/AFP/kernel_matrix/' + name_ds +'/KM_test_tanimoto/KM_tanimoto_CKSAAP_test.csv', delimiter = ',')

        kernel_list = [f1, f2, f3, f4, f5, f6, f7]
        typed_kernel_list = List()
        [typed_kernel_list.append(x) for x in kernel_list]
        K = combine_kernels(typed_kernel_list, weight_v)
        print(K)

        with open('D:/Study/Bioinformatics/AFP/kernel_matrix_G5+T2/' + name_ds +'/KM_test/combine_' + name_mkl + '_test.csv', 'w', newline='') as csvfile:
            writer = csv.writer(csvfile)
            for row in K:
                writer.writerow(row)
            csvfile.close()
